package com.findfuture.common.util;

/**
 * Created by IntelliJ IDEA.
 * User: Yanggang
 * Date: 12-1-17
 * Time: 下午3:18
 * To change this template use File | Settings | File Templates.
 */

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class FileUploadUtils {


    /**
     * 得到文件后缀  jpg
     *
     * @param fileName
     * @return
     */
    public static String getPostfix(String fileName) {
        if (fileName.equals(""))
            return "";
        int pos = fileName.lastIndexOf(".");
        if (pos < 0) {
            return fileName.substring(fileName.length() - 3).toLowerCase();
        } else {
            return fileName.substring(pos + 1).toLowerCase();
        }
    }

    /**
     * 创建目录
     *
     * @param filePath
     */
    public static void createDir(String filePath) {
        File myFile = new File(filePath);
        if (!myFile.exists()) {
            if (!myFile.mkdirs())
                System.out.println("创建目录 " + myFile.getAbsolutePath() + " fail");
            else
                System.out.println("创建目录 " + myFile.getAbsolutePath() + " success");
        }
        myFile = null;
    }

    /**
     * 等比例缩放图片
     *
     * @param infile
     * @param outfile
     * @param width
     * @param height
     * @param quality
     * @throws IOException
     * @throws InterruptedException
     */
    public static void Thumbnail(String infile, String outfile, int width, int height, int quality) throws IOException, InterruptedException {
        // save thumbnail image to OUTFILE
        //System.out.println("infile:" + infile);
        BufferedImage thumbImage = null;
        BufferedOutputStream out = null;
        Image image = null;
        image = Toolkit.getDefaultToolkit().createImage(infile);
        MediaTracker mediaTracker = new MediaTracker(new Container());
        mediaTracker.addImage(image, 0);
        mediaTracker.waitForID(0);
        int thumbWidth = width;
        int thumbHeight = height;
        double thumbRatio = (double) thumbWidth / (double) thumbHeight;
        int imageWidth = image.getWidth(null);
        int imageHeight = image.getHeight(null);
        double imageRatio = (double) imageWidth / (double) imageHeight;
        if (thumbRatio < imageRatio) {
            thumbHeight = (int) (thumbWidth / imageRatio);
        } else {
            thumbWidth = (int) (thumbHeight * imageRatio);
        }
        thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
        out = new BufferedOutputStream(new FileOutputStream(outfile));
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
        quality = Math.max(0, Math.min(quality, 100));
        param.setQuality((float) quality / 100.0f, false);
        encoder.setJPEGEncodeParam(param);
        encoder.encode(thumbImage);
        out.close();
        thumbImage = null;
        out = null;
        image = null;
    }

    /**
     * 删除项目路径下的文件
     *
     * @param path 相对于项目的路径
     */
    public static void deleteWebAppFile(String path) {
        File dir = new File(System.getProperty("webapp.root") + path);
        if (dir.exists()) {
            System.out.println("文件:[" + dir.getAbsolutePath() + "]删除操作");
            dir.delete();
        }
    }

    /**
     * 递归便利找出所有svn信息文件
     *
     * @param file
     */
    public static void findSvn(File file) {
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isDirectory()) {
                if (f.getName().equals(".svn")) {
                    list.add(f.getAbsolutePath());
                    continue;
                }
                findSvn(f);
            }
        }
    }

    static ArrayList list = new ArrayList();


    /**
     * 删除文件
     *
     * @param file
     */
    private static void deleteFile(File file) {
        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            } else if (file.isDirectory()) {
                File files[] = file.listFiles();
                for (int i = 0; i < files.length; i++) {
                    deleteFile(files[i]);
                }
            }
            file.delete();
        } else {
            System.out.println("所删除的文件不存在！" + '\n');
        }
    }

    /**
     * 删除指定目录下的SVN信息
     * @param directoryPath
     */
    public static void deleteSVNFile(String directoryPath) {
        list = new ArrayList();
        File directory = new File(directoryPath);
        findSvn(directory);

        for (Object o : list) {
            StringUtil.println(o.toString());
            File file = new File(o.toString());
            deleteFile(file);
        }
    }
}
